import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QLabel, QLineEdit, QVBoxLayout


class FormExample(QWidget):
    def __init__(self):
        super().__init__()

        # 创建一个 QFormLayout 实例
        self.form_layout = QFormLayout()

        # 添加标签和对应的输入控件到 QFormLayout
        self.form_layout.addRow(QLabel("姓名:"), QLineEdit())
        self.form_layout.addRow(QLabel("年龄:"), QLineEdit())
        self.form_layout.addRow(QLabel("地址:"), QLineEdit())

        # 如果需要，可以设置标签的对齐方式
        self.form_layout.setLabelAlignment(Qt.AlignRight)

        # 创建一个垂直布局，将 QFormLayout 添加进去
        main_layout = QVBoxLayout()
        main_layout.addLayout(self.form_layout)

        # 设置窗口的主布局
        self.setLayout(main_layout)

        # 设置窗口标题和大小
        self.setWindowTitle('QFormLayout 示例')
        self.setGeometry(300, 300, 300, 200)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = FormExample()
    ex.show()
    sys.exit(app.exec_())